package cn.message.web;

import cn.itcast.pojo.horsemanWeightAndSum;
import cn.itcast.pojo.order;
import cn.itcast.pojo.orderItem;
import cn.itcast.pojo.recycleDetail;
import cn.itcast.util.OutputStringController;
import cn.message.service.IShowChartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 郭智超
 * @create 2020-04-16 9:26
 */
@Slf4j
@RestController
@RequestMapping(value="/dhn/message",produces="text/html;charset=UTF-8")
public class ShowChartController {

    @Resource
    private IShowChartService showChartService;

    @Resource
    OutputStringController osc;

//    @RequestMapping("/getLineChart")
//    @ResponseBody
//    public String getLineChart(){
//        String results = "";
//        List<recycleDetail> lineChart = showChartService.getLineChart();
//        HashMap<String, Object> map = new HashMap<String,Object>();
//        ArrayList<Object> dateList = new ArrayList<>();
//        ArrayList<Object> totalWeightList = new ArrayList<>();
//        if(lineChart.size() != 0){
//            for (recycleDetail recycleDetail : lineChart) {
//                totalWeightList.add(recycleDetail.getTotalWeight());
//                dateList.add(recycleDetail.getRecycleDate_string());
//            }
//            map.put("dateList",dateList);
//            map.put("totalWeightList",totalWeightList);
//        }
//
//        if(lineChart.size() == 0){
//            return osc.failure("数据查询失败");
//        }else {
//            results = osc.resultSuccess("数据查询成功",map);
//        }
//        return results;
//    }

    /**
     * 获取五月份每日货量
     * @return
     */
    @RequestMapping("/getEverydayCommodity")
    @ResponseBody
    public String getEverydayCommodity(){
        String results = "";
        //五月份每日货量
        List<orderItem> everydayCommodity = showChartService.getEverydayCommodity();

        //封装数据
        HashMap<String, Object> map = new HashMap<String,Object>();
        ArrayList<Object> dateList = new ArrayList<>();
        ArrayList<Object> totalWeightList = new ArrayList<>();

        if(everydayCommodity.size() != 0){
            for (orderItem orderItem : everydayCommodity) {
                totalWeightList.add(orderItem.getEveryDayWeight());
                dateList.add(orderItem.getEveryDayTime());
            }
            map.put("dateList",dateList);
            map.put("totalWeightList",totalWeightList);
        }
        if(everydayCommodity.size() == 0){
            return osc.failure("五月份每日货量数据查询失败");
        }else {
            results = osc.resultSuccess("五月份每日货量数据查询成功",map);
        }
        return results;
    }

//    @RequestMapping("/getBarChart")
//    @ResponseBody
//    public String getBarChart(){
//        String results = "";
//        List<recycleDetail> barChart = showChartService.getBarChart();
//        ArrayList<Map<String, Object>> list = new ArrayList<>();
//
//        if(barChart.size() != 0){
//            for(recycleDetail recycleDetail : barChart){
//                HashMap<String,Object> map = new HashMap<String,Object>();
//                map.put("product",recycleDetail.getHorsemanName());
//                map.put("总重量",recycleDetail.getTotalWeight());
//                map.put("总金额",recycleDetail.getTotalPrice());
//                list.add(map);
//            }
//        }
//        if(barChart.size() == 0){
//            return osc.failure("数据查询失败");
//        }else {
//            results = osc.resultSuccess("数据查询成功",list);
//        }
//        return results;
//    }

    /**
     * 每一个骑手的收货重量量和收货金额
     * @return
     */
    @RequestMapping("/getHorsemanWeightAndSum")
    @ResponseBody
    public String getHorsemanWeightAndSum(){
        String results = "";
        List<horsemanWeightAndSum> horsemanWeightAndSumList = showChartService.getHorsemanWeightAndSum();

        //开始封装数据
        ArrayList<Map<String, Object>> list = new ArrayList<>();
        if(horsemanWeightAndSumList.size() != 0){
            for (horsemanWeightAndSum horsemanWeightAndSum : horsemanWeightAndSumList) {
                HashMap<String,Object> map = new HashMap<String,Object>();
                map.put("product",horsemanWeightAndSum.getHorsemanName());
                map.put("总重量（公斤）",horsemanWeightAndSum.getTotalWeight());
                map.put("总金额（元）",horsemanWeightAndSum.getTotalPayMoney());
                list.add(map);
            }
        }
        results = osc.resultSuccess("骑手货量与金额",list);
        return results;
    }

    /**
     * 获取用户下单次数分布
     * @return
     */
    @RequestMapping("/getOrderCountDistribution")
    @ResponseBody
    public String getOrderCountDistribution(){
        String results = "";
        //用户下单次数分布
        List<order> orderCountDistribution = showChartService.orderCountDistribution();
        int totalCount = 0;
        for (order order : orderCountDistribution) {
            totalCount = totalCount + order.getOrderCountNumber();
        }

        for (order order : orderCountDistribution) {
            order.setOrderCountPercentage(new BigDecimal((float)(order.getOrderCountNumber()*100)/totalCount).setScale(0, BigDecimal.ROUND_HALF_UP).doubleValue());
        }

        //开始封装数据
        ArrayList<Object> list = new ArrayList<>();
        ArrayList<Integer> orderCountList = new ArrayList<>();
        ArrayList<Double> orderCountPercentageList = new ArrayList<>();
        if(orderCountDistribution.size() != 0){
            for (order order : orderCountDistribution) {
                orderCountList.add(order.getOrderCount());
                orderCountPercentageList.add(order.getOrderCountPercentage());
            }
            list.add(orderCountList);
            list.add(orderCountPercentageList);
        }
        results = osc.resultSuccess("用户下单次数分布",list);
        return results;
    }

    @RequestMapping("/getPieChart")
    @ResponseBody
    public String getPieChart(){
        String results = "";
//        List<recycleDetail> pieChart = showChartService.getPieChart();
//        ArrayList<Object> dataList = new ArrayList<>();
//        ArrayList<Object> valueList = new ArrayList<>();
//        ArrayList<Object> nameList = new ArrayList<>();
//
//        if(pieChart.size() != 0){
//            for(recycleDetail recycleDetail : pieChart){
//                nameList.add(recycleDetail.getHorsemanName());
//                HashMap<String,Object> map = new HashMap<String,Object>();
//                map.put("name",recycleDetail.getHorsemanName());
//                map.put("value",recycleDetail.getTotalWeight());
//                valueList.add(map);
//            }
//            dataList.add(valueList);
//            dataList.add(nameList);
//        }
//        if(pieChart.size() == 0){
//            return osc.failure("数据查询失败");
//        }else {
//            results = osc.resultSuccess("数据查询成功",dataList);
//        }

        List<horsemanWeightAndSum> horsemanWeightAndSumList = showChartService.getHorsemanWeightAndSum();
        ArrayList<Object> dataList = new ArrayList<>();
        ArrayList<Object> valueList = new ArrayList<>();
        ArrayList<Object> nameList = new ArrayList<>();
        if(horsemanWeightAndSumList.size() != 0){
            for (horsemanWeightAndSum horsemanWeightAndSum : horsemanWeightAndSumList) {
                nameList.add(horsemanWeightAndSum.getHorsemanName());
                HashMap<String,Object> map = new HashMap<String,Object>();
                map.put("name",horsemanWeightAndSum.getHorsemanName());
                map.put("value",horsemanWeightAndSum.getTotalWeight());
                valueList.add(map);
            }
            dataList.add(valueList);
            dataList.add(nameList);
        }
        if(horsemanWeightAndSumList.size() == 0){
            return osc.failure("数据查询失败");
        }else {
            results = osc.resultSuccess("数据查询成功",dataList);
        }
        return results;

    }

    /**
     * 获取回收重量类型分布
     * @return
     */
    @RequestMapping("/getWeightDistribution")
    @ResponseBody
    public String getWeightDistribution(){
        String results = "";
        //回收重量类型分布
        List<orderItem> weightDistribution = showChartService.weightDistribution();

        ArrayList<Object> dataList = new ArrayList<>();
        ArrayList<Object> valueList = new ArrayList<>();
        ArrayList<Object> typeList = new ArrayList<>();
        if(weightDistribution.size() != 0){
            for (orderItem orderItem : weightDistribution) {
                typeList.add(orderItem.getProductType());
                HashMap<String,Object> map = new HashMap<String,Object>();
                map.put("name",orderItem.getProductType());
                map.put("value",orderItem.getProductWeight());
                valueList.add(map);
            }
            dataList.add(valueList);
            dataList.add(typeList);
        }
        if(weightDistribution.size() == 0){
            return osc.failure("数据查询失败");
        }else {
            results = osc.resultSuccess("数据查询成功",dataList);
        }
        return results;
    }

    /**
     * 系统成交总重量
     * @return
     */
    @RequestMapping("/getTotalWeight")
    @ResponseBody
    public String getTotalWeight(){
        String results = "";
        //系统成交总重量
        Double totalWeight = showChartService.getTotalWeight();
        results = osc.resultSuccess("系统成交总重量",totalWeight);
        return results;
    }

    /**
     * 系统成交总金额
     * @return
     */
    @RequestMapping("/getTotalSum")
    @ResponseBody
    public String getTotalSum(){
        String results = "";
        //系统成交总金额
        Double totalSum = showChartService.getTotalSum();
        results = osc.resultSuccess("系统成交总金额",totalSum);
        return results;
    }

    /**
     * 累计下单次数
     * @return
     */
    @RequestMapping("/getOrderCount")
    @ResponseBody
    public String getOrderCount(){
        String results = "";
        //累计下单次数
        int orderCount = showChartService.getOrderCount();
        results = osc.resultSuccess("累计下单次数",orderCount);
        return results;
    }

    /**
     * 累计用户人数
     * @return
     */
    @RequestMapping("/getUserCount")
    @ResponseBody
    public String getUserCount(){
        String results = "";
        //累计用户人数
        int userCount = showChartService.getUserCount();
        results = osc.resultSuccess("累计用户人数",userCount);
        return results;
    }

    /**
     * 获取回收区域重量分布
     * @return
     */
    @RequestMapping("/getWeightCountyDistribution")
    @ResponseBody
    public String getWeightCountyDistribution(){
        String results = "";
        //回收区域重量分布
        List<order> weightCountyDistribution = showChartService.weightCountyDistribution();
        results = osc.resultSuccess("回收区域重量分布",weightCountyDistribution);
        return results;
    }



    @RequestMapping("/getMaxCommodity")
    @ResponseBody
        public String getMaxCommodity(){
        String results = "";
        double maxCommodity = showChartService.getMaxCommodity();
        results = osc.resultSuccess("日最高货量",maxCommodity);
        return results;
    }

//    @RequestMapping("/getBigData")
//    @ResponseBody
//    public String getBigData(){
//        String results = "";
//
//        //系统成交总重量
//        Double totalWeight = showChartService.getTotalWeight();
//
//        //系统成交总金额
//        Double totalSum = showChartService.getTotalSum();
//
//        //获取五月份每日货量
//        List<orderItem> everydayCommodity = showChartService.getEverydayCommodity();
//
//        //累计下单次数
//        int orderCount = showChartService.getOrderCount();
//
//        //累计用户人数
//        int userCount = showChartService.getUserCount();
//
//        //用户下单次数分布
//        List<order> orderCountDistribution = showChartService.orderCountDistribution();
//
//        //获取回收重量类型分布
//        List<orderItem> weightDistribution = showChartService.weightDistribution();
//
//        //回收区域重量分布
//        List<order> weightCountyDistribution = showChartService.weightCountyDistribution();
//
//        HashMap<String, Object> resultMap = new HashMap<>();
//        resultMap.put("totalWeight",totalWeight);
//        resultMap.put("totalSum",totalSum);
//        resultMap.put("everydayCommodity",everydayCommodity);
//        resultMap.put("orderCount",orderCount);
//        resultMap.put("userCount",userCount);
//        resultMap.put("orderCountDistribution",orderCountDistribution);
//        resultMap.put("weightCountyDistribution",weightCountyDistribution);
//        resultMap.put("weightDistribution",weightDistribution);
//
//        results = osc.resultSuccess("yes",resultMap);
//        return results;
//    }

//    //从用户地址表中查询用户的区地址插入到订单表中的区地址字段
//    @RequestMapping("/insertOrderDistrict")
//    @ResponseBody
//    public String insertOrderDistrict(){
//        String results = "";
//        showChartService.insertOrderDistrict();
//        return results;
//    }
}
